package net.lab1024.sa.base.module.support.message.controller;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import net.lab1024.sa.base.common.controller.SupportBaseController;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.domain.R;
import net.lab1024.sa.base.common.util.SmartRequestUtil;

import net.lab1024.sa.base.module.support.message.domain.MessageQueryForm;
import net.lab1024.sa.base.module.support.message.domain.MessageVO;
import net.lab1024.sa.base.module.support.message.service.MessageService;
import org.springframework.web.bind.annotation.*;

/**
 * 消息
 *
 * @author luoyi
 * @date 2024/06/22 20:20
 */
@RestController
// 消息相关接口
public class MessageController extends SupportBaseController {

    @Resource
    private MessageService messageService;

    // 分页查询我的消息 @luoyi
    @PostMapping("/message/queryMyMessage")
    public R<PageResult<MessageVO>> query(@RequestBody @Valid MessageQueryForm queryForm) {
        RequestUser user = SmartRequestUtil.getRequestUser();
        if(user == null){
            return R.userErrorParam("用户未登录");
        }

        queryForm.setSearchCount(false);
        queryForm.setReceiverUserId(user.getUserId());
        queryForm.setReceiverUserType(user.getUserType().getValue());
        return R.ok(messageService.query(queryForm));
    }

    // 查询未读消息数量 @luoyi
    @GetMapping("/message/getUnreadCount")
    public R<Long> getUnreadCount() {
        RequestUser user = SmartRequestUtil.getRequestUser();
        if(user == null){
            return R.userErrorParam("用户未登录");
        }
        return R.ok(messageService.getUnreadCount(user.getUserType(), user.getUserId()));
    }

    // 更新已读 @luoyi
    @GetMapping("/message/read/{messageId}")
    public R<String> updateReadFlag(@PathVariable Long messageId) {
        RequestUser user = SmartRequestUtil.getRequestUser();
        if(user == null){
            return R.userErrorParam("用户未登录");
        }

        messageService.updateReadFlag(messageId, user.getUserType(), user.getUserId());
        return R.ok();
    }

}
